import request from './request'

// 权限管理API
export const permissionApi = {
  // 创建权限
  createPermission(data) {
    return request({
      url: '/permissions',
      method: 'post',
      data
    })
  },

  // 更新权限
  updatePermission(id, data) {
    return request({
      url: `/permissions/${id}`,
      method: 'put',
      data
    })
  },

  // 删除权限
  deletePermission(id) {
    return request({
      url: `/permissions/${id}`,
      method: 'delete'
    })
  },

  // 根据ID获取权限
  getPermissionById(id) {
    return request({
      url: `/permissions/${id}`,
      method: 'get'
    })
  },

  // 分页查询权限
  getPermissions(params) {
    return request({
      url: '/permissions',
      method: 'get',
      params
    })
  },

  // 获取所有权限（树形结构）
  getAllPermissionsTree() {
    return request({
      url: '/permissions/tree',
      method: 'get'
    })
  },

  // 获取菜单权限树
  getMenuPermissionsTree() {
    return request({
      url: '/permissions/menu-tree',
      method: 'get'
    })
  },

  // 获取操作权限列表
  getOperationPermissions() {
    return request({
      url: '/permissions/operations',
      method: 'get'
    })
  },

  // 获取权限的角色列表
  getPermissionRoles(id) {
    return request({
      url: `/permissions/${id}/roles`,
      method: 'get'
    })
  },

  // 批量更新权限状态
  batchUpdatePermissionStatus(permissionIds, status) {
    return request({
      url: '/permissions/batch-status',
      method: 'put',
      params: { status },
      data: permissionIds
    })
  },

  // 复制权限
  copyPermission(id, newPermissionCode, newPermissionName) {
    return request({
      url: `/permissions/${id}/copy`,
      method: 'post',
      params: { newPermissionCode, newPermissionName }
    })
  },

  // 根据用户ID获取权限列表
  getUserPermissions(userId) {
    return request({
      url: `/permissions/user/${userId}`,
      method: 'get'
    })
  },

  // 检查用户是否有指定权限
  hasPermission(userId, permissionCode) {
    return request({
      url: '/permissions/check',
      method: 'get',
      params: { userId, permissionCode }
    })
  },

  // 检查用户是否有指定资源和操作的权限
  hasResourcePermission(userId, resource, action) {
    return request({
      url: '/permissions/check-resource',
      method: 'get',
      params: { userId, resource, action }
    })
  }
}
